package cn.edu.njust.service.impl;

import cn.edu.njust.core.UserContext;
import cn.edu.njust.core.enums.RoleEnum;
import cn.edu.njust.core.enums.SignInStatusEnum;
import cn.edu.njust.core.exception.ParamsException;
import cn.edu.njust.mapper.UserMapper;
import cn.edu.njust.pojo.bo.PageBean;
import cn.edu.njust.pojo.po.User;
import cn.edu.njust.service.UserService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Override
    public void updateSignInStatusById(Integer id, SignInStatusEnum status) {
        userMapper.updateSignInStatus(status.getVal(), id);
    }

    @Override
    public PageBean<User> selectUserList(Integer currentPage, Integer pageSize, User user) {
        List<String> roles = ListUtil.of(RoleEnum.COUNSELOR.getDesc(), RoleEnum.MONITOR.getDesc(),
            RoleEnum.SECRETARY.getDesc(), RoleEnum.STUDENT.getDesc());
        List<User> rows = userMapper.selectList(currentPage, pageSize, user, roles);
        int totalCount = userMapper.selectTotalCount(roles, null);
        return PageBean.of(totalCount, rows);
    }

    @Override
    public void deleteByIds(Collection<Integer> ids) {
        if (CollUtil.isEmpty(ids)) {
            throw new ParamsException("删除失败，待删除用户的id不能为空");
        }
        userMapper.deleteByIds(ids);
    }

    @Override
    public PageBean<User> selectByPageAndCondition(int currentPage, int pageSize, User user) {
        int begin = (currentPage - 1) * pageSize;
        List<String> roles = ListUtil.of(RoleEnum.MONITOR.getDesc(), RoleEnum.SECRETARY.getCode(), RoleEnum.STUDENT.getCode());
        List<User> rows = userMapper.selectList(begin, pageSize, user, roles);
        int totalCount = userMapper.selectTotalCount(roles, null);
        return PageBean.of(totalCount, rows);
    }

    @Override
    public PageBean<User> selectStudentInLeaves(int currentPage, int pageSize) {
        int pageNum = (currentPage - 1) * pageSize;
        User currentUser = UserContext.getCurrentUser();
        String classNumber = currentUser.getClassNumber();
        List<User> rows = userMapper.selectListByClass(pageNum, pageSize, false, classNumber);
        int totalCount = userMapper.selectTotalCount(Collections.emptyList(), classNumber);
        return PageBean.of(totalCount, rows);
    }

    @Override
    public void punchIn() {
        userMapper.punchIn(UserContext.getCurrentUserId(), SignInStatusEnum.SIGNED.getVal());
    }

    @Override
    public void resetPasswordByIds(List<Integer> ids) {

        if (CollUtil.isEmpty(ids)) {
            log.warn("重置用户列表为空");
            return;
        }
        userMapper.resetPasswordByIds(ids);
        log.info("重置成功，用户id为{}", ids);
    }
}
